#include <bits/stdc++.h>
using namespace std;
const int N = 30, M = 3e4 + 10;

int n, m;
int p[N], v[N];
int f[M];

int main()
{
	cin >> m >> n;
	for(int i = 1;i <= n;i ++) cin >> p[i] >> v[i];
	for(int i = 1;i <= n;i ++)
		for(int j = m;j >= p[i];j --)
			f[j] = max(f[j], f[j - p[i]] + p[i] * v[i]);
	cout << f[m] << endl;
	return 0;
}